package com.mh.controller;

import com.mh.pojo.GarbageResult;
import com.mh.pojo.StatisticsVO;
import com.mh.service.StatisticalService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Date:2025/3/15
 * author:zmh
 * description: 统计的相关接口
 **/

@RestController
@RequestMapping("/statistical")
@CrossOrigin
@RequiredArgsConstructor
@Slf4j
public class StatisticalController {

    private final StatisticalService statisticalService;

    private final MongoTemplate mongoTemplate;

    @GetMapping("/test")
    public String test() {
        String databaseName = mongoTemplate.getDb().getName();
        System.out.println("Connected to MongoDB database: " + databaseName);
        Object obj = mongoTemplate.findOne(new Query(), Object.class, "garbage_recognition");
        System.out.println(obj);
        List<Document> allData = mongoTemplate.findAll(Document.class, "garbage_recognition");
        System.out.println("All Data: " + allData);
        List<GarbageResult> all = mongoTemplate.findAll(GarbageResult.class);
        return "test";
    }

    /**
     * 获取分类统计数据
     * @return ·
     */
    @GetMapping("/countByResult")
    public ResponseEntity<List<StatisticsVO>> countByResult() {
        return ResponseEntity.ok(statisticalService.countByResult());
    }

    /**
     * 获取物品统计数据
     * @return ·
     */
    @GetMapping("/countByItem")
    public ResponseEntity<List<StatisticsVO>> countByItem() {
        return ResponseEntity.ok(statisticalService.countByItem());
    }

    /**
     * 新增数据
     * @param garbageResult 数据对象
     * @return ·
     */
    @PostMapping
    public ResponseEntity<GarbageResult> create(@RequestBody GarbageResult garbageResult) {
        return ResponseEntity.status(HttpStatus.CREATED)
                .body(statisticalService.save(garbageResult));
    }

    /**
     * 删除数据
     * @param id 数据id
     * @return ·
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> delete(@PathVariable String id) {
        statisticalService.deleteById(id);
        return ResponseEntity.noContent().build();
    }

    /**
     * 更新数据
     * @param garbageResult 数据对象
     * @return ·
     */
//    @PutMapping
//    public ResponseEntity<GarbageResult> update(@RequestBody GarbageResult garbageResult) {
//        GarbageResult updated = statisticalService.update(garbageResult);
//        return updated != null ?
//                ResponseEntity.ok(updated) :
//                ResponseEntity.notFound().build();
//    }

}
